/**
 * @file wa_panel.c
 *
 */

 /*********************
  *      INCLUDES
  *********************/
#include "wa_conf.h"
#if USE_WA_PANEL != 0
#include "wa_panel.h"

  /*********************
  *      DEFINES
  *********************/

  /**********************
  *      TYPEDEFS
  **********************/

  /**********************
  *  STATIC PROTOTYPES
  **********************/

static wa_obj_callback ancestor_callback;
static void wa_panel_callback(wa_sign* sign);

/**********************
 *  STATIC VARIABLES
 **********************/


/**********************
*      MACROS
**********************/

/**********************
*   GLOBAL FUNCTIONS
**********************/

/**
* panel module init
*/
void wa_panel_module_init(void)
{
}

/**
* create a panel
* @param par
* @return panel obj
*/
wa_obj* wa_panel_create(wa_obj* par)
{
	wa_obj* new_panel;
	wa_panel_ext* panel_ext;

	new_panel = wa_obj_create(par, NULL);
	if (new_panel == NULL)
	{
		return new_panel;
	}
	if (ancestor_callback == NULL)
	{
		ancestor_callback = wa_obj_get_callback(new_panel);
	}

	panel_ext = wa_obj_malloc_ext_attr(new_panel, sizeof(wa_panel_ext));
	if (panel_ext == NULL)
	{
		return NULL;
	}

	panel_ext->cb = NULL;
	panel_ext->color = WA_COLOR_BACKGROUND;

	wa_obj_set_callback(new_panel, wa_panel_callback);

	return new_panel;
}

/**
* panel set callback
* @param panel
* @param cb
*/
void wa_panel_set_callback(wa_obj* panel, wa_obj_callback cb)
{
	wa_panel_ext* ext;
	ext = wa_obj_get_ext_attr(panel);
	ext->cb = cb;
}

/**
* panel set color
* @param panel
* @param color
*/
void wa_panel_set_color(wa_obj* panel, wa_gui_color color)
{
	wa_panel_ext* ext;
	ext = wa_obj_get_ext_attr(panel);
	ext->color = color;
	wa_obj_invalidate((wa_obj*)panel);
}

/**
* panel get color
* @param panel
* @param color
*/
wa_gui_color wa_panel_get_color(wa_obj* panel)
{
	wa_panel_ext* ext;
	ext = wa_obj_get_ext_attr(panel);
	return ext->color;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

 /**
 * panel obj redraw function
 * @param obj
 */
static void _Paint(wa_obj* obj)
{
	wa_panel_ext* ext;
	ext = wa_obj_get_ext_attr(obj);

	wa_mask_fillrect(obj->rect.x1, obj->rect.y1,
		wa_obj_get_width(obj),
		wa_obj_get_height(obj),
		&(obj->redraw_rect),
		ext->color);
}

/**
 * panel obj delete function
 * @param obj
 */
static void _Delete(wa_obj* obj)
{
}

/**
 * panel obj callback function
 * @param obj
 */
static void _Callback(wa_obj* obj, wa_sign* sign)
{
	wa_panel_ext* ext;
	ext = wa_obj_get_ext_attr(obj);
	if (ext->cb != NULL)
	{
		(ext->cb)(sign);
	}
}

/**
 * panel obj callback function
 * @param sign
 */
static void wa_panel_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;

	ancestor_callback(sign);
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_Paint(obj);
			break;
		}
		case WA_SIGNAL_DELETE:
		{
			_Delete(obj);
			break;
		}
		default:
		{
			break;
		}
	}

	_Callback(obj, sign);
}


#endif
